home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / indents.zip / comment.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  17.3 KB  |  412 lines

  1. /**
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that the above copyright notice and this paragraph are duplicated in all
  9.  * such forms and that any documentation, advertising materials, and other
  10.  * materials related to such distribution and use acknowledge that the
  11.  * software was developed by the University of California, Berkeley, the
  12.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  13.  * either University or Sun Microsystems may not be used to endorse or
  14.  * promote products derived from this software without specific prior written
  15.  * permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  17.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include "globals.h"
  21.  
  22. #ifndef lint
  23. # ifndef ANSIC
  24. static char     sccsid[] = "@(#)comment.c    6.0 (Berkeley) 92/06/15";
  25. # endif         /* ANSIC */
  26. #endif          /* not lint */
  27.  
  28.  
  29. /**
  30.  * NAME: pr_comment
  31.  *
  32.  * FUNCTION: This routine takes care of scanning and printing comments.
  33.  *
  34.  * ALGORITHM: 1) Decide where the comment should be aligned, and if lines should
  35.  * be broken. 2) If lines should not be broken and filled, just copy up to
  36.  * end of comment. 3) If lines should be filled, then scan through input_buffer
  37.  * copying characters to com_buf.  Remember where the last blank, tab, or
  38.  * new line was.  When line is filled, print up to last blank and continue
  39.  * copying.
  40.  *
  41.  * HISTORY: November 1976, D A Willcox of CAC, Initial coding.
  42.  * 12/6/76, D A Willcox of CAC, Modification to handle UNIX-style comments.
  43.  */
  44.  
  45.  
  46. /* this routine processes comments.  It makes an attempt to keep comments
  47.  * from going over the max. line length.  If a line is too long, it moves
  48.  * everything from the last blank to the next comment line.  Blanks and tabs
  49.  * from the beginning of the input line are removed */
  50.  
  51.  
  52. #ifdef ANSIC
  53. void            pr_comment(char *combuf)
  54. #else           /* ANSIC */
  55. pr_comment()
  56.     char           *combuf;
  57. #endif          /* ANSIC */
  58. {
  59.     int             now_col;    /* column we are in now */
  60.     int             adj_max_col;/* Adjusted max_col for when we decide to
  61.                                  * spill comments over the right margin */
  62.     char           *last_bl = NULL; /* points to the last blank in the output
  63.                                      * buffer */
  64.     char           *t_ptr = NULL;   /* used for moving string */
  65.     int             unix_comment;   /* tri-state variable used to decide if
  66.                                      * it is a UNIX-style comment. 0 means
  67.                                      * only blanks since '/''*', 1 means
  68.                                      * regular style comment, 2 means UNIX
  69.                                      * style comment */
  70.     int             break_delim = comment_delimiter_on_blankline;
  71.     int             l_just_saw_decl = ps.just_saw_decl;
  72.  
  73. #if 0
  74.     int             ps.last_nl = 0; /* true iff the last significant thing
  75.                                      * we've seen is a newline */
  76. #endif          /* 0 */
  77.     int             one_liner = 1;  /* true iff this comment is a one-liner */
  78.  
  79.     adj_max_col = max_col;
  80.     ps.just_saw_decl = 0;
  81.     last_bl = 0;                /* no blanks found so far */
  82.     ps.box_com = false;         /* at first, assume that we are not in a
  83.                                  * boxed comment or some other comment that
  84.                                  * should not be touched */
  85.     ++ps.out_coms;              /* keep track of number of comments */
  86.     unix_comment = 1;           /* set flag to let us figure out if there is
  87.                                  * a unix-style comment ** DISABLED: use 0 to
  88.                                  * re-enable this hack! */
  89.  
  90.     /* Figure where to align and how to treat the comment */
  91.  
  92.     if (ps.col_1 && !format_col1_comments) {    /* if comment starts in
  93.                                                  * column 1 it should not be
  94.                                                  * touched */
  95.         ps.box_com = true;
  96.         ps.com_col = 1;
  97.     } else {
  98.         if (*buf_ptr == '-' || *buf_ptr == '*') {
  99.             ps.box_com = true;  /* a comment with a '-' or '*' immediately
  100.                                  * after the '/''*' is assumed to be a boxed
  101.                                  * comment */
  102.             break_delim = 0;
  103.         }
  104.         if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
  105.             /* klg: check only if this line is blank */
  106.             /* If this (*and previous lines are*) blank, don't put comment
  107.              * way out at left */
  108.             ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  109.             adj_max_col = block_comment_max_col;
  110.             if (ps.com_col <= 1)
  111.                 ps.com_col = 1 + !format_col1_comments;
  112.         } else {
  113.             register int    target_col;
  114.  
  115.             break_delim = 0;
  116.             if (s_code != e_code)
  117.                 target_col = count_spaces(compute_code_target(), s_code);
  118.             else {
  119.                 target_col = 1;
  120.                 if (s_lab != e_lab)
  121.                     target_col = count_spaces(compute_label_target(), s_lab);
  122.             }
  123.             ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
  124.             if (ps.com_col < target_col)
  125.                 ps.com_col = target_col +
  126.                     (tabsize - ((target_col - 1) % tabsize));   /* JHT 22oct89 */
  127.             if (else_or_endif) {/* -cp PETER */
  128.                 ps.com_col = ps.else_endif_col; /* -cp PETER */
  129.                 else_or_endif = false;  /* -cp PETER */
  130.                 /* We want the comment to appear one space after the #else or
  131.                  * #endif.                         -cp PETER */
  132.                 if (ps.com_col < target_col)    /* -cp PETER */
  133.                     ps.com_col = target_col + 1;    /* -cp PETER */
  134.             }                   /* -cp PETER */
  135.             if (ps.com_col + 24 > adj_max_col)
  136.                 adj_max_col = ps.com_col + 24;
  137.         }
  138.     }
  139.     if (ps.box_com) {
  140.         buf_ptr[-2] = 0;
  141.         ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
  142.         buf_ptr[-2] = '/';
  143.     } else {
  144.         ps.n_comment_delta = 0;
  145.         while (*buf_ptr == ' ' || *buf_ptr == '\t')
  146.             buf_ptr++;
  147.     }
  148.     ps.comment_delta = 0;
  149.     *e_com++ = '/';             /* put '/''*' into buffer */
  150.     if (ps.cc_comment)          /* (or '/''/') */
  151.         *e_com++ = '/';
  152.     else
  153.         *e_com++ = '*';
  154.     if (*buf_ptr != ' ' && !ps.box_com)
  155.         *e_com++ = ' ';
  156.  
  157.     *e_com = '\0';
  158.     if (troff) {
  159.         now_col = 1;
  160.         adj_max_col = 80;
  161.     } else
  162.         now_col = count_spaces(ps.com_col, s_com);  /* figure what column we
  163.                                                      * would be in if we
  164.                                                      * printed the comment
  165.                                                      * now */
  166.  
  167.     /* Start to copy the comment */
  168.  
  169.     while (1) {                 /* this loop will go until the comment is
  170.                                  * copied */
  171.         if (*buf_ptr >= 040 && *buf_ptr != '*')
  172.             ps.last_nl = 0;
  173.         check_size(com);
  174.         switch (*buf_ptr) {     /* this checks for various spcl cases */
  175.         case 014:               /* check for a form feed */
  176.             if (!ps.box_com) {  /* in a text comment, break the line here */
  177.                 ps.use_ff = true;
  178.                 /* fix so dump_line uses a form feed */
  179.                 dump_line();
  180.                 last_bl = 0;
  181.                 *e_com++ = ' ';
  182.                 *e_com++ = '*';
  183.                 *e_com++ = ' ';
  184.                 while (*++buf_ptr == ' ' || *buf_ptr == '\t');
  185.             } else {
  186.                 if (++buf_ptr >= buf_end)
  187.                     fill_buffer();
  188.                 *e_com++ = 014;
  189.             }
  190.             break;
  191.  
  192.         case '\n':
  193.             if (ps.cc_comment) {
  194.                 *e_com = '\0';
  195.                 dump_line();
  196.                 ps.cc_comment = 0;
  197.                 ps.just_saw_decl = l_just_saw_decl; /* ?? */
  198.                 if (++buf_ptr >= buf_end)   /* eat '\n' */
  199.                     fill_buffer();
  200.                 ps.last_nl = 1; /* jrs 12 Mar 92 */
  201.                 return;
  202.             }
  203.             if (had_eof) {      /* check for unexpected eof */
  204.                 printf("Un-terminated comment\n");
  205.                 *e_com = '\0';
  206.                 dump_line();
  207.                 return;
  208.             }
  209.             one_liner = 0;
  210.             if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, we
  211.                                              * don't ignore the newline */
  212.                 if (s_com == e_com) {
  213.                     *e_com++ = ' ';
  214.                     *e_com++ = ' ';
  215.                 }
  216.                 *e_com = '\0';
  217.                 if (!ps.box_com && e_com - s_com > 3) {
  218.                     if (break_delim == 1 && s_com[0] == '/'
  219.                         && s_com[1] == '*' && s_com[2] == ' ') {
  220.                         char           *t = e_com;
  221.  
  222.                         break_delim = 2;
  223.                         e_com = s_com + 2;
  224.                         *e_com = 0;
  225.                         if (blanklines_before_blockcomments)
  226.                             prefix_blankline_requested = 1;
  227.                         dump_line();
  228.                         e_com = t;
  229.                         s_com[0] = s_com[1] = s_com[2] = ' ';
  230.                     }
  231.                     dump_line();
  232.                     check_size(com);
  233.                     *e_com++ = ' ';
  234.                     *e_com++ = ' ';
  235.                 }
  236.                 dump_line();
  237.                 now_col = ps.com_col;
  238.             } else {
  239.                 ps.last_nl = 1;
  240.                 if (unix_comment != 1) {    /* we are not in unix_style
  241.                                              * comment */
  242.                     if (unix_comment == 0 && s_code == e_code) {
  243.                         /* if it is a UNIX-style comment, ignore the
  244.                          * requirement that previous line be blank for
  245.                          * un-indention */
  246.                         ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  247.                         if (ps.com_col <= 1)
  248.                             ps.com_col = 2;
  249.                     }
  250.                     unix_comment = 2;   /* permanently remember that we are
  251.                                          * in this type of comment */
  252.                     dump_line();
  253.                     ++line_no;
  254.                     now_col = ps.com_col;
  255.                     *e_com++ = ' ';
  256.                     /* fix so that the star at the start of the line will
  257.                      * line up */
  258.                     do          /* flush leading white space */
  259.                         if (++buf_ptr >= buf_end)
  260.                             fill_buffer();
  261.                     while (*buf_ptr == ' ' || *buf_ptr == '\t');
  262.                     break;
  263.                 }
  264.                 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
  265.                     last_bl = e_com - 1;
  266.                 /* if there was a space at the end of the last line, remember
  267.                  * where it was */
  268.                 else {          /* otherwise, insert one */
  269.                     last_bl = e_com;
  270.                     check_size(com);
  271.                     *e_com++ = ' ';
  272.                     ++now_col;
  273.                 }
  274.             }
  275.             ++line_no;          /* keep track of input line number */
  276.             if (!ps.box_com) {
  277.                 int             nstar = 1;
  278.  
  279.                 do {            /* flush any blanks and/or tabs at start of
  280.                                  * next line */
  281.                     if (++buf_ptr >= buf_end)
  282.                         fill_buffer();
  283.                     if (*buf_ptr == '*' && --nstar >= 0) {
  284.                         if (++buf_ptr >= buf_end)
  285.                             fill_buffer();
  286.                         if (*buf_ptr == '/')
  287.                             goto end_of_comment;
  288.                     }
  289.                 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  290.             } else if (++buf_ptr >= buf_end)
  291.                 fill_buffer();
  292.             break;              /* end of case for newline */
  293.  
  294.         case '*':               /* must check for possibility of being at end
  295.                                  * of comment */
  296.             if (++buf_ptr >= buf_end)   /* get to next char after * */
  297.                 fill_buffer();
  298.  
  299.             if (unix_comment == 0)  /* set flag to show we are not in
  300.                                      * unix-style comment */
  301.                 unix_comment = 1;
  302.  
  303.             if (*buf_ptr == '/') {  /* it is the end!!! */
  304.         end_of_comment:
  305.                 if (++buf_ptr >= buf_end)
  306.                     fill_buffer();
  307.  
  308.                 if (*(e_com - 1) != ' ' && !ps.box_com) {   /* ensure blank before
  309.                                                              * end */
  310.                     *e_com++ = ' ';
  311.                     ++now_col;
  312.                 }
  313.                 if (break_delim == 1 && !one_liner && s_com[0] == '/'
  314.                     && s_com[1] == '*' && s_com[2] == ' ') {
  315.                     char           *t = e_com;
  316.  
  317.                     break_delim = 2;
  318.                     e_com = s_com + 2;
  319.                     *e_com = 0;
  320.                     if (blanklines_before_blockcomments)
  321.                         prefix_blankline_requested = 1;
  322.                     dump_line();
  323.                     e_com = t;
  324.                     s_com[0] = s_com[1] = s_com[2] = ' ';
  325.                 }
  326.                 if (break_delim == 2 && e_com > s_com + 3
  327.                      /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
  328.                     *e_com = '\0';
  329.                     dump_line();
  330.                     now_col = ps.com_col;
  331.                 }
  332.                 check_size(com);
  333.                 *e_com++ = '*';
  334.                 *e_com++ = '/';
  335.                 *e_com = '\0';
  336.                 ps.just_saw_decl = l_just_saw_decl;
  337.                 return;
  338.             } else {            /* handle isolated '*' */
  339.                 *e_com++ = '*';
  340.                 ++now_col;
  341.             }
  342.             break;
  343.         default:                /* we have a random char */
  344.             if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
  345.                 unix_comment = 1;   /* we are not in unix-style comment */
  346.  
  347.             *e_com = *buf_ptr++;
  348.             if (buf_ptr >= buf_end)
  349.                 fill_buffer();
  350.  
  351.             if (*e_com == '\t') /* keep track of column */
  352.                 now_col = now_col + (tabsize - ((now_col - 1) % tabsize));
  353.             else if (*e_com == '\b')    /* this is a backspace */
  354.                 --now_col;
  355.             else
  356.                 ++now_col;
  357.  
  358.             if (*e_com == ' ' || *e_com == '\t')
  359.                 last_bl = e_com;/* remember we saw a blank */
  360.  
  361.             ++e_com;
  362.             if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
  363.                 /* the comment is too long, it must be broken up */
  364.                 if (break_delim == 1 && s_com[0] == '/'
  365.                     && s_com[1] == '*' && s_com[2] == ' ') {
  366.                     char           *t = e_com;
  367.  
  368.                     break_delim = 2;
  369.                     e_com = s_com + 2;
  370.                     *e_com = 0;
  371.                     if (blanklines_before_blockcomments)
  372.                         prefix_blankline_requested = 1;
  373.                     dump_line();
  374.                     e_com = t;
  375.                     s_com[0] = s_com[1] = s_com[2] = ' ';
  376.                 }
  377.                 if (last_bl == 0) { /* we have seen no blanks */
  378.                     last_bl = e_com;    /* fake it */
  379.                     *e_com++ = ' ';
  380.                 }
  381.                 *e_com = '\0';  /* print what we have */
  382.                 *last_bl = '\0';
  383.                 while (last_bl > s_com && last_bl[-1] < 040)
  384.                     *--last_bl = 0;
  385.                 e_com = last_bl;
  386.                 dump_line();
  387.  
  388.                 *e_com++ = ps.cc_comment ? '/' : ' ';   /* blanks or slashes */
  389.                 *e_com++ = ps.cc_comment ? '/' : ' ';   /* for continuation */
  390.                 *e_com++ = ' ';
  391.  
  392.                 t_ptr = last_bl + 1;
  393.                 last_bl = 0;
  394.                 if (t_ptr >= e_com) {
  395.                     while (*t_ptr == ' ' || *t_ptr == '\t')
  396.                         t_ptr++;
  397.                     while (*t_ptr != '\0') {    /* move unprinted part of
  398.                                                  * comment down in buffer */
  399.                         if (*t_ptr == ' ' || *t_ptr == '\t')
  400.                             last_bl = e_com;
  401.                         *e_com++ = *t_ptr++;
  402.                     }
  403.                 }
  404.                 *e_com = '\0';
  405.                 now_col = count_spaces(ps.com_col, s_com);  /* recompute current
  406.                                                              * position */
  407.             }
  408.             break;
  409.         }
  410.     }
  411. }
  412.